﻿Public Class FTPenilaian



    'member variables
    Private _cMataPelajaran As CMataPelajaran
    Private _cguru As CGuru
    Private _ckelas As CKelas
    Private _guru As tbl_guru
    Private _kelas As tbl_kelas
    Private _mataPelajaran As tbl_mata_pelajaran
    Public Property IdGuru As Integer
    Public Property TanggalUjian As Date
    Public Property KategoriUjian As Integer
    Public Property IdKelas As Integer
    Public Property IdMataPelajaran As Integer
    Public Property IdSemester As Integer
    Private _cnilaisiswa As CNilaiSiswa
    Private _dataNilai As DataTable
    Private _idPenilaian As Integer


    Public Sub New(idGuru As Integer, tanggalUjian As Date, kategoriUjian As Integer, idKelas As Integer, idMataPelajaran As Integer, idsemester As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.IdGuru = idGuru
        Me.TanggalUjian = tanggalUjian
        Me.KategoriUjian = kategoriUjian
        Me.IdKelas = idKelas
        Me.IdMataPelajaran = idMataPelajaran
        Me.IdSemester = idsemester
    End Sub

    ''' <summary>
    ''' instance form untuk perubahan nilai
    ''' </summary>
    ''' <param name="idGuru"></param>
    ''' <param name="tanggalUjian"></param>
    ''' <param name="kategoriUjian"></param>
    ''' <param name="idKelas"></param>
    ''' <param name="idMataPelajaran"></param>
    ''' <param name="idsemester"></param>
    ''' <param name="dataNilai">id_ssw,nama,nil</param>
    ''' <remarks></remarks>
    Public Sub New(idGuru As Integer, tanggalUjian As Date, kategoriUjian As Integer, idKelas As Integer, idMataPelajaran As Integer, idsemester As Integer, idpenilaian As Integer, dataNilai As DataTable)
        Me.New(idGuru, tanggalUjian, kategoriUjian, idKelas, idMataPelajaran, idsemester)
        _dataNilai = dataNilai
        _idPenilaian = idPenilaian

    End Sub



    Private Sub FTPenilaian_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set dgmaster
        DGMaster.AutoGenerateColumns = False

        'set member variables 
        _cguru = New CGuru
        _guru = _cguru.getById(Me.IdGuru)
        lblGuru.Text = _guru.nama

        _ckelas = New CKelas
        _kelas = _ckelas.getById(Me.IdKelas)
        lblKelas.Text = _kelas.nama

        lblTanggal.Text = Me.TanggalUjian.ToShortDateString

        lblKategori.Text = IIf(Me.KategoriUjian = 1, "UAS", IIf(Me.KategoriUjian = 2, "UTS", "Tugas/Ulangan Harian"))

        _cMataPelajaran = New CMataPelajaran
        _mataPelajaran = _cMataPelajaran.getById(Me.IdMataPelajaran)
        lblMataPelajaran.Text = _mataPelajaran.nama

        'tampilkn data siswa
        _cnilaisiswa = New CNilaiSiswa
        If _dataNilai Is Nothing Then
            'nilai baru
            DGMaster.DataSource = _cnilaisiswa.getDaftarSiswa(Me.IdSemester, _kelas.id)
        Else
            'tampilkan data perubahan
            lblPerubahan.Visible = True
            btnSimpan.Visible = False
            btnSimpanPerubahan.Visible = True
            DGMaster.DataSource = _cnilaisiswa.getNilaiSiswa(_idPenilaian)
        End If

        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btnkeluar.Click
        Me.Close()
    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1
    End Sub

    Private Sub btnSimpan_Click(sender As System.Object, e As System.EventArgs) Handles btnSimpan.Click
        'BUAT DATA TABLE NILAI
        Dim dTab As New DataTable
        dTab.Columns.Add("id_ssw")
        dTab.Columns.Add("nil")

        'isi datatable dari datagrid
        For Each arow As DataGridViewRow In DGMaster.Rows
            Dim dRow As DataRow = dTab.NewRow
            dRow.Item("id_ssw") = arow.Cells("cid").Value
            dRow.Item("nil") = arow.Cells("cnilai").Value
            dTab.Rows.Add(dRow)
        Next

        Try
            _cnilaisiswa = New CNilaiSiswa
            _idPenilaian = _cnilaisiswa.simpan(_guru.id, IdSemester, _kelas.id, _mataPelajaran.id, KategoriUjian, TanggalUjian, dTab)

            MsgBox("Data nilai tersimpan")
            btnSimpan.Visible = False
            btnSimpanPerubahan.Visible = True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    Private Sub btnSimpanPerubahan_Click(sender As System.Object, e As System.EventArgs) Handles btnSimpanPerubahan.Click
        'simpan perubahan

        'BUAT DATA TABLE NILAI
        Dim dTab As New DataTable
        dTab.Columns.Add("id_ssw")
        dTab.Columns.Add("nil")

        'isi datatable dari datagrid
        For Each arow As DataGridViewRow In DGMaster.Rows
            Dim dRow As DataRow = dTab.NewRow
            dRow.Item("id_ssw") = arow.Cells("cid").Value
            dRow.Item("nil") = arow.Cells("cnilai").Value
            dTab.Rows.Add(dRow)
        Next

        Try
            _cnilaisiswa = New CNilaiSiswa
            _cnilaisiswa.Update(_idPenilaian, dTab)

            MsgBox("Perubahan data nilai tersimpan")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
End Class